package com.hadwinling.schedule;

import cn.hutool.core.io.file.FileReader;
import com.hadwinling.entity.Burn;
import com.hadwinling.entity.ShikeTime;
import com.hadwinling.service.RealTimeService;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname IoTDBShedule 定时任务遗弃，暂不要
 * @Description TODO
 * @Date 2021/7/10 11:27
 * @Created by
 */
//@Component
public class IoTDBShedule {
    @Autowired
    private RealTimeService realTimeService;

    @Autowired
    private Session iotdbSession;

    int line = 0;
    int rhb = 0;
    int shike = 0;

    @Scheduled(cron = "0/10 * * * * ?")//每10秒执行一次
    public void schedule() throws StatementExecutionException, IoTDBConnectionException {
//        FileReader fileReader = new FileReader("D:\\MySoftWare\\PCWare\\Tencent\\AppCache\\WetChatCache\\WeChat Files\\wxid_ysw3yj1tpdwp22\\FileStorage\\File\\2021-06\\go30");
        FileReader fileReader = new FileReader("/home/hedian/go30");

        List<String> lines = fileReader.readLines();
        if (line == lines.size()) {
            line = 0;
        }

        long timestamp = System.currentTimeMillis();

        String[] shujukuaiArray = {"gl", "kq", "rq", "s", "d", "wd"}; //gl:功率 kq：快群 rq：热群 s: 氙 d: 碘 wd：温度

        List<String> deviceIds = new ArrayList<>();
        List<List<String>> measurementsList = new ArrayList<>();
        List<List<Object>> valuesList = new ArrayList<>();
        List<Long> timestamps = new ArrayList<>();
        List<List<TSDataType>> typesList = new ArrayList<>();

        List<String> measurements = new ArrayList<>();
        List<TSDataType> types = new ArrayList<>();
        for (int i = 0; i < 32; i++) {
            measurements.add("s" + (i + 1));
            types.add(TSDataType.FLOAT);
        }

        for (int zb = 0; zb < 6; zb++) {// 6 个指标

            for (int zj = 0; zj < 52; zj++) { // 52 个组件-

                String deviceId = "root.dc_dx2." + shujukuaiArray[zb] + ".zj" + zj;
                List<Object> values = new ArrayList<>();
                final String[] s = lines.get(line).split(" ");
                for (int i = 0; i < 32; i++) {
                    String value = s[i];
                    values.add(Float.parseFloat(value));
                }
                line++;
                deviceIds.add(deviceId);
                valuesList.add(values);
                measurementsList.add(measurements);
                typesList.add(types);
                timestamps.add(timestamp);
            }
            try {
                iotdbSession.insertRecords(deviceIds, timestamps, measurementsList, typesList, valuesList);
            } catch (IoTDBConnectionException e) {
                e.printStackTrace();
            } catch (StatementExecutionException e) {
                e.printStackTrace();
            }

            deviceIds.clear();
            measurementsList.clear();
            valuesList.clear();
            timestamps.clear();
        }
        System.out.println(line);
    }


    public void ScheudleImpl(){
        FileReader fileReader = new FileReader("D:\\MySoftWare\\PCWare\\Tencent\\AppCache\\WetChatCache\\WeChat Files\\wxid_ysw3yj1tpdwp22\\FileStorage\\File\\2021-06\\go30");
//        FileReader fileReader = new FileReader("/home/hedian/go30");

        List<String> lines = fileReader.readLines();
        if (line == lines.size()) {
            line = 0;
        }

        long timestamp = System.currentTimeMillis();

        String[] shujukuaiArray = {"gl", "kq", "rq", "s", "d", "wd"}; //gl:功率 kq：快群 rq：热群 s: 氙 d: 碘 wd：温度

        List<String> deviceIds = new ArrayList<>();
        List<List<String>> measurementsList = new ArrayList<>();
        List<List<Object>> valuesList = new ArrayList<>();
        List<Long> timestamps = new ArrayList<>();
        List<List<TSDataType>> typesList = new ArrayList<>();

        List<String> measurements = new ArrayList<>();
        List<TSDataType> types = new ArrayList<>();
        for (int i = 0; i < 32; i++) {
            measurements.add("s" + (i + 1));
            types.add(TSDataType.FLOAT);
        }

        for (int zb = 0; zb < 6; zb++) {// 6 个指标

            for (int zj = 0; zj < 52; zj++) { // 52 个组件-

                String deviceId = "root.dc_dx2." + shujukuaiArray[zb] + ".zj" + zj;
                List<Object> values = new ArrayList<>();
                final String[] s = lines.get(line).split(" ");
                for (int i = 0; i < 32; i++) {
                    String value = s[i];
                    values.add(Float.parseFloat(value));
                }
                line++;
                deviceIds.add(deviceId);
                valuesList.add(values);
                measurementsList.add(measurements);
                typesList.add(types);
                timestamps.add(timestamp);
            }
            try {
                iotdbSession.insertRecords(deviceIds, timestamps, measurementsList, typesList, valuesList);
            } catch (IoTDBConnectionException e) {
                e.printStackTrace();
            } catch (StatementExecutionException e) {
                e.printStackTrace();
            }

            deviceIds.clear();
            measurementsList.clear();
            valuesList.clear();
            timestamps.clear();
        }
        System.out.println(line);
    }


    public void IotDBSchedule() {
        FileReader fileReader = new FileReader("D:\\MySoftWare\\PCWare\\Tencent\\AppCache\\WetChatCache\\WeChat Files\\wxid_ysw3yj1tpdwp22\\FileStorage\\File\\2021-06\\go30");
//        FileReader fileReader = new FileReader("/home/hedian/go30");

        List<String> lines = fileReader.readLines();
        int line = 0;
        long timestamp = System.currentTimeMillis();

        String[] shujukuaiArray = {"gl", "kq", "rq", "s", "d", "wd"}; //gl:功率 kq：快群 rq：热群 s: 氙 d: 碘 wd：温度

        List<String> deviceIds = new ArrayList<>();
        List<List<String>> measurementsList = new ArrayList<>();
        List<List<Object>> valuesList = new ArrayList<>();
        List<Long> timestamps = new ArrayList<>();
        List<List<TSDataType>> typesList = new ArrayList<>();

        List<String> measurements = new ArrayList<>();
        List<TSDataType> types = new ArrayList<>();
        for (int i = 0; i < 32; i++) {
            measurements.add("s" + (i + 1));
            types.add(TSDataType.FLOAT);
        }

        for (; rhb < 18; rhb++) {

            for (; shike < 98; shike++) { // 98 个时刻

                for (int zb = 0; zb < 6; zb++) {// 6 个指标

                    for (int zj = 0; zj < 52; zj++) { // 52 个组件-

                        String deviceId = "root.dc_dx2." + shujukuaiArray[zb] + ".zj" + zj;
                        List<Object> values = new ArrayList<>();
                        final String[] s = lines.get(line).split(" ");
                        for (int i = 0; i < 32; i++) {
                            String value = s[i];
                            values.add(Float.parseFloat(value));
                        }
                        line++;
                        deviceIds.add(deviceId);
                        valuesList.add(values);
                        measurementsList.add(measurements);
                        typesList.add(types);
                        timestamps.add(timestamp);
                    }
                    try {
                        iotdbSession.insertRecords(deviceIds, timestamps, measurementsList, typesList, valuesList);
                    } catch (IoTDBConnectionException e) {
                        e.printStackTrace();
                    } catch (StatementExecutionException e) {
                        e.printStackTrace();
                    }

                    deviceIds.clear();
                    measurementsList.clear();
                    valuesList.clear();
                    timestamps.clear();

                }
                timestamp = timestamp + 1000; // 1秒一个时刻
            }

        }
    }


}
